package com.zxy.supplier_system.server.repository

import com.zxy.supplier_system.server.entity.*
import com.zxy.supplier_system.server.utils.SpecificationUtils
import com.zxy.supplier_system.server.utils.joinOnce
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice
import org.springframework.data.jpa.domain.Specification
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor
import org.springframework.data.jpa.repository.Query
import java.time.LocalDate

interface ProcurementReturnRepository : JpaRepository<ProcurementReturn, Int>, JpaSpecificationExecutor<ProcurementReturn> {

    @Query("select count(id) from ProcurementReturn where date(createdDateTime) = current_date() order by createdDateTime desc limit 1 ")
    fun countToday(): Int

    fun findByIdAndSupplier_SystemService(id: Int, supplier_systemService: SystemService,): ProcurementReturn?

    fun findAllBySupplier_Id(supplierId:Int, pageable: Pageable): Slice<ProcurementReturn>

    object Specifications {

        fun marketSaleManagerEqual(employeeId: Int): Specification<ProcurementReturn> {
            return Specification { root, _, criteriaBuilder ->
                MarketRepository.Specifications.saleManagerEqual(root.joinOnce("market"), criteriaBuilder, employeeId)
            }
        }

        fun marketShoppingGuidesContains(employeeId: Int): Specification<ProcurementReturn> {
            return Specification { root, query, criteriaBuilder ->
                MarketRepository.Specifications.shoppingGuidesContains(
                    root.joinOnce("market"),
                    criteriaBuilder,
                    query ,
                    employeeId
                )
            }
        }

        fun startDateGreaterThanOrEqualTo(startDate: LocalDate?): Specification<ProcurementReturn> {
            return Specification { root, _, criteriaBuilder ->
                startDate?.let {
                    criteriaBuilder.greaterThanOrEqualTo(root.get("createdDateTime"), it)
                }
            }
        }

        fun endDateLessThanOrEqualTo(endDate: LocalDate?): Specification<ProcurementReturn> {
            return Specification { root, _, criteriaBuilder ->
                endDate?.let {
                    criteriaBuilder.lessThanOrEqualTo(root.get("createdDateTime"), it)
                }
            }
        }

        fun supplierIn(marketIds: List<Int>): Specification<ProcurementReturn> {
            return SpecificationUtils.`in`({ root ->
                root.joinOnce<ProcurementReturn, Market>("supplier").get("id")
            }, marketIds)
        }

        fun createdByIn(employeeIds: List<Int>): Specification<ProcurementReturn> {
            return SpecificationUtils.`in`({ root ->
                root.joinOnce<ProcurementReturn, Employee>("createdBy").get("id")
            }, employeeIds)
        }

//        fun statusEqual(status:ProcurementReturn.Status?):Specification<ProcurementReturn>{
//            return Specification{root, query, criteriaBuilder ->
//                if (status==null){
//                    null
//                }else{
//                    val submitDateTimePath = root.get<OffsetDateTime>("submittedDateTime")
//                    when (status) {
//                        ProcurementReturn.Status.WAIT_SUBMIT -> {
//                            criteriaBuilder.isNull(submitDateTimePath)
//                        }
//                        ProcurementReturn.Status.SUBMITTED -> {
//                            criteriaBuilder.and(criteriaBuilder.isNotNull(submitDateTimePath),criteriaBuilder.isNull(root.joinOnce<ProcurementReturn,ProcurementReturnReceipt>("receipt",JoinType.LEFT)))
//                        }
//                        else -> {
//                            criteriaBuilder.and(criteriaBuilder.isNotNull(root.joinOnce<ProcurementReturn,ProcurementReturnReceipt>("receipt",JoinType.LEFT)))
//                        }
//                    }
//                }
//            }
//        }
    }


}

